import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;


public class SortEasy2Files {
    
    enum Sort {Sort, Unsort};
    
    public static void main(String[] args) {
        if (args.length != 2)
            System.out.print("You have entered the wrong parameters");
        else {
            String path = args[0];
            String sort = args[1];
            //System.out.println(Sort.values().toString());
            //System.out.println(sort);
            if (sort.equals(Sort.Unsort.toString()) || sort.equals(Sort.Sort.toString()))
            readerFile(path, sort);
            else {
                System.out.print("You have entered the wrong parameters (enter 'Sort' or 'Unsort')"); 
            }
        }
    }
    
    public static void readerFile(String nameFile1, String sortFile1) {
        try {
            FileReader fr = new FileReader (nameFile1);
            Scanner scan = new Scanner(fr);
            ArrayList<String> word = new ArrayList<String>();
            String word1;
            String str;
            while (scan.hasNext()) {
                   str=scan.next();
                   word1 = wordInArray(str);
                   word.add(word1);
            }
            
            parseStringUnsort(word, sortFile1);
            
            fr.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
            }
        catch (IOException e) {
            e.printStackTrace(System.err);
        }
        }
    
    public static String wordInArray(String strParse) {
        
        String delemiters = " \t\n\r\f,.!?—{}#/:;[]()-";
        StringTokenizer tokenizer = new StringTokenizer(strParse, delemiters);
        String word2 = null;   
        
        while (tokenizer.hasMoreTokens()) {
            word2 = tokenizer.nextToken().toLowerCase();
        }
        return word2;
        
    }
    
    public static void show(Map<String, Integer> words){
        Set<String> keys = words.keySet();
        System.out.println("Array size: " + words.size());
        System.out.println("Array : ");
                for (String r : keys) {
                System.out.println(r + " " + words.get(r));
                }
            }
    
 
    
    public static void show(String[] word){
        System.out.println("Array size: " + word.length);
        System.out.println("Array : ");
                for (String r : word) {
                System.out.println(r);
                }
            } 
    
   public static String parseStringUnsort(ArrayList<String> word2, String sort) {       

       String []arr = new String [word2.size()];
       String tmp = null;
       Map<String, Integer> wordss = new TreeMap<String, Integer>();
       Map<String, Integer> wordsu = new HashMap<String, Integer>(); 
    
       for (int i=0; i<word2.size(); i++)
       {
           if (word2.get(i)!=null) {
           boolean res = true;
           int count = 0;
           tmp = word2.get(i);
           for (int p=0; p<arr.length; p++)
               {
               if (tmp.equalsIgnoreCase(arr[p]))
               {
                   res = false;
                   }
               }
           for (int j=0; j<word2.size(); j++)
               {
               if (tmp.equalsIgnoreCase(word2.get(j)) && res == true) 
                   {
                   count++;
                   arr[i] = tmp;
                   }
               }
           
          
           if (word2.get(i) != null && count != 0)
           {  
               if (sort.equals(Sort.Sort.toString())) {
               wordss.put(arr[i], count);  
               /**
                * System.out.print(count + " ");
                * System.out.println(arr[i]);
                **/
               }
               else wordsu.put(arr[i], count);
               }
           }
           
           //на печать
          // Set<String> keys = words.keySet();
          //for (String key : keys) {
           //    System.out.println(key + " " + words.get(key));
            //   }

    }
       
       if (sort.equals(Sort.Sort.toString())) {
           show(wordss);  
             }
           else show(wordsu);
       
       
    return tmp;
       
   }
   
 }
